<?php 

class carController extends appController
{
	
	public function clear()
	{
		unset($_SESSION['xywxid']);
		unset($_SESSION['wxid']);
		unset($_SESSION);
	}
	
    
	/*
	 * @author zzx
	 * @desc 摇一摇时触发，异步返回中奖信息
	 * res.data.status 1=>中奖  0=>谢谢参与   2=>非法操作
	 */
	public function ajaxPrize()
	{
	    $res = array(
	        "error"=>1,
	        "content"=>"",
	        data=>array()
	        
	    );
	    
	    
	    
	    if(!$_SESSION['openid'])
	    {
	        $res['content'] = "非法操作!";
	        $res['error'] = 2;
	    }
	    else
	    {
	        $prize = Model("car")->getPrize($_SESSION['openid']);
	        
	        if($prize['id'] == 0){
	            $res['error'] = 0;
	            $res['content'] = "谢谢参与!";
	        }else{
	            $res['data'] = $prize;
	        }
	        
	    }
	    
	    die(json_encode($res));
	    
	    
	}
	
	public function ajaxCard()
	{
	   
	     
	    $prize_user_id = $_REQUEST['id']>0?intval($_REQUEST['id']):0;

	    
	    
	    $cardList = Model('car')->getCard($prize_user_id);
	     
	     
	    if(!$cardList||!$prize_user_id)
	    {
	        die("非法操作");
	         
	    }
	     
	    $jsdk_card = Model('car')->build_jsdk_card_car($cardList);
	       
// 	   var_dump($jsdk_card);

	    echo json_encode($jsdk_card);
	   
	}
	

	public function index()
	{
	    
	    $res = $this->getOpenidByShake();
	     
	    if($res['errcode']==0&&$res['data']['openid'])
	    {
	        
	        $_SESSION['openid'] = $res['data']['openid'];
	    }
	    else
	    {
	        echo("非法操作！errmsg：".$res['errmsg']);
	    }
// 	    echo $res['data']['openid'];
// 	    var_dump($res);
	    
	    
	    $this->assign_global($config);
	    
	    $this->display("index.html");
	}
    
	public function shake()
	{
	    
	    //$_SESSION['openid'] = "asdfsdgkfgjkd23123skjalsdfj";
// 	    echo $_SESSION['openid'];
	
	    $this->assign_global($config);
	
	    $jsdk = Model('car')->get_jsdk_car();
	
	    $this->assign("jsdk", $jsdk);
	
	    $this->display("shake.html");
	
	}
	
	
	public function shake2()
	{
	     
	    $_SESSION['openid'] = $_GET['openid'];
	    // 	    echo $_SESSION['openid'];
	
	    $this->assign_global($config);
	
	    $jsdk = Model('car')->get_jsdk_car();
	
	    $this->assign("jsdk", $jsdk);
	
	    $this->display("shake.html");
	
	}
	
	public function card()
	{
	
	    $prize_user_id = $_REQUEST['id']>0?intval($_REQUEST['id']):0;
	     
	    
	    $prize_user = Model('car')->getPrizeUserRecord($prize_user_id);
	    
	    
	    if(!$prize_user)
	    {
	        die("非法操作");
	    
	    }
	   
	   
	    $this->assign_global($config);
	
	    $jsdk = Model('car')->get_jsdk_car();
	
	    $this->assign("jsdk", $jsdk);
	   
	    $this->assign("prize_user", $prize_user);
        
		$this->display("card.html");
	
	}
		
	// 领取卡券后异步回调
	public function getCard()
	{
	    $id = $_REQUEST['id']?$_REQUEST['id']:0;
        
	    if($id == 0)
	    {
	        die("error".$id);
	    }
	    
	    Model('car')->saveCard($id);

	    echo "success".$id;
	    
	}
	
	
	/**
	 * 摇一摇周边获取openid以及设备信息
	 */
	public function getOpenidByShake()
	{
	    
	    $access_token = Model('car')->getAccessTokenJsCar();
// 	    echo $access_token;
// 	    $access_token = "vMBXnQJdIsZIaC2-67IMzHNC8BWEKYPmdRAun09gqVIEpKSNA66B72zzrIolOx-wq4p9h44mXwoAJRagPcR1dCl4ESmKN5L9Rji-Li89Ie8BxC7ciR0m839kgV4FTCiAEASGP-C1yYhwWncB_wpnccW-GUALHeAGAKVR";
	    $data['ticket'] = $_GET['ticket'];
	    
	    $url = "https://api.weixin.qq.com/shakearound/user/getshakeinfo?access_token=$access_token";
	    $json = $this->post_it($url, json_encode($data));//获取微信用户基本信息
	    $arr = json_decode($json,true);
	    return $arr;
	}
	
    
	/**
	 * 用post方法获取数据
	 * @param $url
	 * @param $data
	 * @return
	 */
	public function post_it($url, $data)
	{
	    $curl = curl_init();
	    curl_setopt($curl, CURLOPT_URL, $url);
	    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
	    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
	    curl_setopt($curl, CURLOPT_POST, 1);
	    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
	    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	    $result = curl_exec($curl);
	    if(curl_errno($curl))
	    {
	        return 'Errno'.curl_error($curl);
	    }
	    curl_close($curl);
	    return $result;
	}
	
}

?>
